﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;
using EFong.Contract;

namespace EFong.View.Customer
{
    public partial class Frm_PassengerEdit : BasicForm
    {
        public Frm_PassengerEdit()
        {
            InitializeComponent();
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Save":
                    this.save();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        private void save()
        {
            Passenger passenger = getPassenger();

            UIOperManager.SaveData(
                () =>
                {
                    if (string.IsNullOrEmpty(passenger.CName) && string.IsNullOrEmpty(passenger.EName))
                    {
                        MsgBox.Info("请输入乘客的 中文名 或 英文名 .");
                        return false;
                    }

                    return true;
                },
                () =>
                {
                    updateToCurPass(passenger);
                    return true;
                },
                string.Empty,
                () =>
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                );
        }

        //将控件的值转为passenger
        private Passenger getPassenger()
        {
            return new Passenger
            {
                CName = this.txt_CName.GetText(),
                EName = this.txt_EName.GetText(),
                Sex = this.cmb_Sex.GetDisplay(),
                BirthDay = this.dte_Birthday.GetValue(),
                Nation = this.txt_Nation.GetText(),
                Tel1 = this.txt_Tel1.GetText(),
                Tel2 = this.txt_Tel2.GetText(),
                IDCardType1 = this.cmb_CardType1.GetDisplay(),
                IDCardNr1 = this.txt_CardNr1.GetText(),
                IDCardType2 = this.cmb_CardType2.GetDisplay(),
                IDCardNr2 = this.txt_CardNr2.GetText(),
                FlightCard1 = this.txt_FlightCard1.GetText(),
                FlightCard2 = this.txt_FlightCard2.GetText(),
                FlightCard3 = this.txt_FlightCard3.GetText(),
                IDCardEffective1 = this.txt_CardEffective1.GetText(),
                IDCardEffective2 = this.txt_CardEffective2.GetText()
            };
        }

        //更新passenger的值到current passenger
        private void updateToCurPass(Passenger passenger)
        {
            //if (CurPass == null)
            //{
            //    CurPass = passenger;
            //}
            //else
            //{
                CurPass.CName = passenger.CName;
                CurPass.EName = passenger.EName;
                CurPass.Sex = passenger.Sex;
                CurPass.BirthDay = passenger.BirthDay;
                CurPass.Nation = passenger.Nation;
                CurPass.Tel1 = passenger.Tel1;
                CurPass.Tel2 = passenger.Tel2;
                CurPass.IDCardType1 = passenger.IDCardType1;
                CurPass.IDCardNr1 = passenger.IDCardNr1;
                CurPass.IDCardType2 = passenger.IDCardType2;
                CurPass.IDCardNr2 = passenger.IDCardNr2;
                CurPass.FlightCard1 = passenger.FlightCard1;
                CurPass.FlightCard2 = passenger.FlightCard2;
                CurPass.FlightCard3 = passenger.FlightCard3;
                CurPass.Effective = passenger.Effective;
                CurPass.IDCardEffective1 = passenger.IDCardEffective1;
                CurPass.IDCardEffective2 = passenger.IDCardEffective2;
            //}
        }

        // current passenger
        private Passenger _curPass;
        public Passenger CurPass
        {
            get { return _curPass; }
            set
            {
                _curPass = value;
                onCurPassChanged();
            }
        }

        private void onCurPassChanged()
        {
            if (CurPass == null)
            {
                // clear passenger datas
            }
            else
            {
                // set passenger datas
                this.txt_CName.SetText(CurPass.CName);
                this.txt_EName.SetText(CurPass.EName);
                this.cmb_Sex.SetDisplay(CurPass.Sex);
                this.dte_Birthday.SetValue(CurPass.BirthDay);
                this.txt_Nation.SetText(CurPass.Nation);
                this.txt_Tel1.SetText(CurPass.Tel1);
                this.txt_Tel2.SetText(CurPass.Tel2);
                this.cmb_CardType1.SetDisplay(CurPass.IDCardType1);
                this.txt_CardNr1.SetText(CurPass.IDCardNr1);
                this.cmb_CardType2.SetDisplay(CurPass.IDCardType2);
                this.txt_CardNr2.SetText(CurPass.IDCardNr2);
                this.txt_FlightCard1.SetText(CurPass.FlightCard1);
                this.txt_FlightCard2.SetText(CurPass.FlightCard2);
                this.txt_FlightCard3.SetText(CurPass.FlightCard3);
                this.txt_CardEffective1.SetText(CurPass.IDCardEffective1);
                this.txt_CardEffective2.SetText(CurPass.IDCardEffective2);
            }
        }
    }
}
